﻿using System;
using System.Collections;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation
{
	public class ReferenceCurveDrawingKit
	{
		public static void DrawProfile(UIApplication uiApp, LevelProfiles profiles)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			ReferenceCurveDrawingKit.DeleteOldCurve(document, false);
			CurveArray curveArray = new CurveArray();
			foreach (Profile profile in profiles.Profiles)
			{
				foreach (EdgeInfo edgeInfo in profile.EdgeLoop)
				{
					Curve curve = edgeInfo.ConvertTo();
					curveArray.Append(curve);
				}
			}
			if (uiApp.ActiveUIDocument.ActiveView.GetType() == typeof(View3D))
			{
				Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(XYZ.Zero, XYZ.BasisX, XYZ.BasisY);
				SketchPlane sketchPlane = SketchPlane.Create(document, plane);
				if (curveArray.Size <= 0)
				{
					return;
				}
				ModelCurveArray modelCurveArray = document.Create.NewModelCurveArray(curveArray, sketchPlane);
				if (!ReferenceCurveDrawingKit.GetLineStyle(document))
				{
					return;
				}
				IEnumerator enumerator3 = modelCurveArray.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						object obj = enumerator3.Current;
						((ModelCurve)obj).LineStyle = ReferenceCurveDrawingKit.m_LineStyle;
					}
					return;
				}
			}
            if (uiApp.ActiveUIDocument.ActiveView.GetType() == typeof(ViewPlan) && uiApp.ActiveUIDocument.ActiveView.ViewType == (ViewType)1 && curveArray.Size > 0)
			{
				DetailCurveArray detailCurveArray = document.Create.NewDetailCurveArray(uiApp.ActiveUIDocument.ActiveView, curveArray);
				if (ReferenceCurveDrawingKit.GetLineStyle(document))
				{
					foreach (object obj2 in detailCurveArray)
					{
						((DetailCurve)obj2).LineStyle = ReferenceCurveDrawingKit.m_LineStyle;
					}
				}
			}
		}

		public static void DrawBox(UIApplication uiApp, XYZ pt1, XYZ pt2, XYZ pt3, XYZ pt4, double height)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			ReferenceCurveDrawingKit.DeleteOldCurve(document, false);
			if (document.ActiveView.GetType() == typeof(View3D))
			{
				ModelCurveArray modelCurveArray = new ModelCurveArray();
				Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(XYZ.Zero, XYZ.BasisX, XYZ.BasisY);
				SketchPlane sketchPlane = SketchPlane.Create(document, plane);
				CurveArray curveArray = new CurveArray();
				curveArray.Append(Line.CreateBound(pt1, pt2));
				curveArray.Append(Line.CreateBound(pt2, pt3));
				curveArray.Append(Line.CreateBound(pt3, pt4));
				curveArray.Append(Line.CreateBound(pt4, pt1));
				foreach (object obj in document.Create.NewModelCurveArray(curveArray, sketchPlane))
				{
					ModelCurve modelCurve = (ModelCurve)obj;
					modelCurveArray.Append(modelCurve);
				}
				XYZ xyz = XYZ.BasisZ * height;
				XYZ xyz2 = pt1 + xyz;
				XYZ xyz3 = pt2 + xyz;
				XYZ xyz4 = pt3 + xyz;
				XYZ xyz5 = pt4 + xyz;
				Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(new XYZ(0.0, 0.0, height), XYZ.BasisX, XYZ.BasisY);
				SketchPlane sketchPlane2 = SketchPlane.Create(document, plane2);
				CurveArray curveArray2 = new CurveArray();
				curveArray2.Append(Line.CreateBound(xyz2, xyz3));
				curveArray2.Append(Line.CreateBound(xyz3, xyz4));
				curveArray2.Append(Line.CreateBound(xyz4, xyz5));
				curveArray2.Append(Line.CreateBound(xyz5, xyz2));
				foreach (object obj2 in document.Create.NewModelCurveArray(curveArray2, sketchPlane2))
				{
					ModelCurve modelCurve2 = (ModelCurve)obj2;
					modelCurveArray.Append(modelCurve2);
				}
				Plane plane3 = RevitVersionFuncs.CreatePlanByOriginAndBasis(pt1, (pt2 - pt1).Normalize(), XYZ.BasisZ);
				SketchPlane sketchPlane3 = SketchPlane.Create(document, plane3);
				CurveArray curveArray3 = new CurveArray();
				curveArray3.Append(Line.CreateBound(pt1, xyz2));
				curveArray3.Append(Line.CreateBound(pt2, xyz3));
				foreach (object obj3 in document.Create.NewModelCurveArray(curveArray3, sketchPlane3))
				{
					ModelCurve modelCurve3 = (ModelCurve)obj3;
					modelCurveArray.Append(modelCurve3);
				}
				Plane plane4 = RevitVersionFuncs.CreatePlanByOriginAndBasis(pt3, (pt4 - pt3).Normalize(), XYZ.BasisZ);
				SketchPlane sketchPlane4 = SketchPlane.Create(document, plane4);
				CurveArray curveArray4 = new CurveArray();
				curveArray4.Append(Line.CreateBound(pt3, xyz4));
				curveArray4.Append(Line.CreateBound(pt4, xyz5));
				foreach (object obj4 in document.Create.NewModelCurveArray(curveArray4, sketchPlane4))
				{
					ModelCurve modelCurve4 = (ModelCurve)obj4;
					modelCurveArray.Append(modelCurve4);
				}
				if (!ReferenceCurveDrawingKit.GetLineStyle(document))
				{
					return;
				}
				IEnumerator enumerator = modelCurveArray.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj5 = enumerator.Current;
						((ModelCurve)obj5).LineStyle = ReferenceCurveDrawingKit.m_LineStyle;
					}
					return;
				}
			}
            if (document.ActiveView.GetType() == typeof(ViewPlan) && document.ActiveView.ViewType == (ViewType)1)
			{
				CurveArray curveArray5 = new CurveArray();
				curveArray5.Append(Line.CreateBound(pt1, pt2));
				curveArray5.Append(Line.CreateBound(pt2, pt3));
				curveArray5.Append(Line.CreateBound(pt3, pt4));
				curveArray5.Append(Line.CreateBound(pt4, pt1));
				DetailCurveArray detailCurveArray = document.Create.NewDetailCurveArray(document.ActiveView, curveArray5);
				if (ReferenceCurveDrawingKit.GetLineStyle(document))
				{
					foreach (object obj6 in detailCurveArray)
					{
						((DetailCurve)obj6).LineStyle = ReferenceCurveDrawingKit.m_LineStyle;
					}
				}
			}
		}

		public static void ReleaseResources(Document doc)
		{
			ReferenceCurveDrawingKit.DeleteOldCurve(doc, true);
			ReferenceCurveDrawingKit.m_LineStyle = null;
		}

		private static void DeleteOldCurve(Document doc, bool createNewTransaction = false)
		{
			Transaction transaction = null;
			if (createNewTransaction)
			{
				transaction = new Transaction(doc);
				transaction.Start("delete old curves");
			}
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(CurveElement)))
			{
				CurveElement curveElement = element as CurveElement;
				if (curveElement != null && curveElement.LineStyle.Name.CompareTo(ReferenceCurveDrawingKit.m_StrLineName) == 0)
				{
					list.Add(element.Id);
				}
			}
			try
			{
				if (list.Count > 0)
				{
					doc.Delete(list);
				}
				if (createNewTransaction)
				{
					transaction.Commit();
				}
			}
			catch (Exception)
			{
				if (createNewTransaction)
				{
					transaction.RollBack();
				}
			}
		}

		private static bool GetLineStyle(Document doc)
		{
			if (ReferenceCurveDrawingKit.m_LineStyle != null)
			{
				return true;
			}
			Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
			if (category == null)
			{
				return false;
			}
			int num = 5;
			Color lineColor = new Color(byte.MaxValue, 0, byte.MaxValue);
			if (!category.SubCategories.Contains(ReferenceCurveDrawingKit.m_StrLineName))
			{
				if (!ReferenceCurveDrawingKit.CreateLineStyle(doc, num, ReferenceCurveDrawingKit.m_StrLineName, lineColor, ref category))
				{
					return false;
				}
			}
			else
			{
				Category category2 = category.SubCategories.get_Item(ReferenceCurveDrawingKit.m_StrLineName);
				category2.LineColor = lineColor;
				category2.SetLineWeight(num, (Autodesk.Revit.DB.GraphicsStyleType)1);
			}
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null && graphicsStyle.Name.CompareTo(ReferenceCurveDrawingKit.m_StrLineName) == 0)
				{
					ReferenceCurveDrawingKit.m_LineStyle = graphicsStyle;
				}
			}
			return ReferenceCurveDrawingKit.m_LineStyle != null;
		}

		private static bool CreateLineStyle(Document doc, int lineWidth, string typeName, Color lineColor, ref Category lineCategory)
		{
			bool result;
			try
			{
				Category category = doc.Settings.Categories.NewSubcategory(lineCategory, typeName);
				category.LineColor = lineColor;
				category.SetLineWeight(lineWidth, (Autodesk.Revit.DB.GraphicsStyleType)1);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private static GraphicsStyle m_LineStyle = null;

		private static string m_StrLineName = "HYProfileLine";
	}
}
